#!/usr/bin/perl
#pvrescan
require './lvm-lib.pl';
&ReadParse();
my $err_msg = 'abc';



sub get_lv_info_b
{
        my @lvinfo;
        my @lvinfob;
        my ($line, $lv, $lvname, $vg, $status, $size, $openmark);
        my ($status_a, $status_o, $lvpath, @lvstat, @sizeinfo, $freesize);
        open(LV, "lvm lvs --noheadings --units m 2>/dev/null |");
        my $i=0;
        while($line=<LV>){
                $line =~ s/\r|\n//g;
                if($line =~ /\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)M/){
                        $lvname = $1;
                        $lv = '/dev/'.$2.'/'.$lvname;
                        $vg = $2;
                        $size = $4;
                        $status = $3;
                        if($status =~ /a/) {
                                $status_a = $text{'lv_available'};
                        }
                        else{
                                $status_a = $text{'lv_unavailable'};
                        }
                        if($status =~ /o/){
                                $status_o = 1;
                        }
                        else{
                                $status_o = 0;
                        }
                        $lvpath = "/usrvol/".$lvname;
                        @lvstat = vol_mount_status($lvpath);
                        $i++;
                }else{
                        next;
                }
                $lvinfo[$i]->{'lv'} = $lv;
                $lvinfo[$i]->{'lvname'} = $lvname;
                $lvinfo[$i]->{'vg'} = $vg;
                $lvinfo[$i]->{'status'} = $status;
                $lvinfo[$i]->{'status_a'} = $status_a;
                $lvinfo[$i]->{'status_o'} = $status_o;
                $lvinfo[$i]->{'size'} = $size;
                $lvinfo[$i]->{'lvpath'} = $lvpath;
                $lvinfo[$i]->{'status_m'} = $lvstat[0];
                $lvinfo[$i]->{'status_i'} = check_istat($lv);
        }
        close(LV);
        foreach $lvi (@lvinfo) {
                if($lvi->{'lv'}) {
                        push(@lvinfob, $lvi);
                }
        }
        rewrite_voltab(@lvinfob);
        return @lvinfob;
}


sub get_sd_info
{
    my ($pvinfo, @pv_info);
    $pvinfo = \@pv_info;
    $i=0;
    open(PV, "/sbin/fdisk -l /dev/sd? 2>/dev/null |");
    while($line = <PV>) {
      $line =~ s/\r|\n//g;
      $line =~ s/#.*$//g;
      if ($line =~ /^Disk\s+(\/dev\/sd\S):\s+\S+\s+GB/) {
        $name = $1;
        $pvinfo->[$i]->{'name'} = $name;
        $i++;
      }
      else {
         next;
      }
    }
    close(PV);
    return @pv_info;
}

sub get_pv_info
{
        my @pvinfo=();
        my @pvinfob;
        my $pv;
        open(PV, "/sbin/lvm pvs --units g --noheadings 2>/dev/null |");
        my $i = 0;
        while($line = <PV>) {
                $line =~ s/\r|\n//g;
                $line =~ s/#.*$//g;
                if ($line =~ /\s+(\S+)\s+(\S+)\s+\S+\s\S+\s+(\S+)G\s+(\S+)G/) {
                        $pv = $1;
                        $i++;
                        $pvinfo[$i]->{'pv'} = $pv;
                }
                elsif($line =~ /\s+(\S+)\s+(\S+)\s+\S+\s\S+\s+(\S+)G\s+0/){
                        $pv = $1;
                        $i++;
                        $pvinfo[$i]->{'pv'} = $pv;
                }
                elsif ($line =~ /\s+(\S+)\s+\S+\s+(\S+)\s+(\S+)G\s+(\S+)G/) {
                        $pv = $1;
                        $i++;
                        $pvinfo[$i]->{'pv'} = $pv;
                }
                else{
                        next;
                }
        }
        close(PV);
        foreach $pvi (@pvinfo) {
                if($pvi->{'pv'}){
                        push(@pvinfob, $pvi);
                }
        }
        return @pvinfob;
}

#main procedure
my @sdinfo;
my $sd;
my @pvinfo;
my $p;
my @lvinfo;
my $lv;
@lvinfo = get_lv_info_b();
@pvinfo = get_pv_info();
@sdinfo = get_sd_info();
my $found;
foreach $lv (@lvinfo) (
	`/sbin/blockdev --setra 16384 $lv->{'lv'}`;
)
foreach $sd (@sdinfo) {
	$found = 0;
	foreach $p (@pvinfo) {
		if ($p->{'pv'} eq $sd->{'name'}){
			$found = 1;
			last;
		}
	}
	if(!$found){
		`/sbin/lvm pvcreate $sd->{'name'}`;
		`/sbin/blockdev --setra 16384 $sd->{'name'}`;
	}	
}
foreach $p (@pvinfo){
	$found = 0;
	foreach $sd (@sdinfo) {
		if($sd->{'name'} eq $p->{'pv'}) {
			$found = 1;
			last;
		}
	}
	if(!$found){
		`/sbin/lvm pvremove $p->{'pv'} -ff -y`;
	}
}


#goto ERROR_OUT;

&redirect("index.cgi");

ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
